
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QClipboard>
#include <QColorDialog>
#include <QTextStream>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
  , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    switchComboBox = ui->switchComboBox;
    pauseStyleBtn = ui->pauseStyleBtn;

    widgets["MainWindow"] = this;
    widgets["QLabel"] = ui->label;
    widgets["QLineEdit"] = ui->lineEdit;
    widgets["QComboBox"] = ui->comboBox;
    widgets["QCheckBox"] = ui->checkBox;
    widgets["QRadioButton"] = ui->radioButton;
    widgets["QPushButton"] = ui->pushButton;
    widgets["QGroupBox"] = ui->groupBox;
        
    // 样式输入框
    connect(ui->styleTextEdit, &QPlainTextEdit::textChanged, this, [=](){
        // 当前控件更新 styleSheet
        m_currentWidget->setStyleSheet(ui->styleTextEdit->toPlainText());

    });
    // 无用的信号
    connect(ui->styleTextEdit, &QPlainTextEdit::blockCountChanged,  this, [=](int newBlockCount){
        // QTextStream(stdout) << newBlockCount << "\n";
    });
    // 样式输入框
    connect(ui->styleTextEdit, &QPlainTextEdit::selectionChanged,  this, [=](){
        // 如果选中了颜色(#xxx或#xyxyxy)，将改变颜色按钮的颜色样式
        QString text = ui->styleTextEdit->textCursor().selectedText().trimmed();
        if (!text.isEmpty()) {
            if (text.length() == 4 || text.length() == 7) {
                if (text.startsWith("#")) {
                    this->setColorBtnColor(text);
                }
            }
        }
    });

    // 控件选择器
    connect(switchComboBox, &QComboBox::currentTextChanged, this, [=](const QString &text){
        // 改变当前控件指针指向的控件
        changeCurrentWidget(text);
        // 更新历史列表为当前控件存储的键列
        ui->historyList->clear();
        if (!widgetStyleMaps[m_currentWidget].isEmpty()) {
            if (widgetStyleMaps[m_currentWidget].keys().length() > 0) {
                foreach(QString key, widgetStyleMaps[m_currentWidget].keys()) {
                    ui->historyList->addItem(key);
                }
            }
        }
    });

    // 去效果的按下
    connect(pauseStyleBtn, &QPushButton::pressed, this, [=](){
        // 当前控制无样式
        m_currentWidget->setStyleSheet("");
    });
    // 抬起操作
    connect(pauseStyleBtn, &QPushButton::released, this, [=](){
        // 假信号: 样式框内容发生改变以触发应用样式
        emit ui->styleTextEdit->textChanged();
    });

    // 字体大小控制
    connect(ui->fontSizeSlider, &QSlider::valueChanged, this, [=](int val){
        // 样式框字体大小改变为传入值
        QFont font = ui->styleTextEdit->font();
        font.setPixelSize(val);
        ui->styleTextEdit->setFont(font);
    });
    // 历史样式列表
    connect(ui->historyList, &QListWidget::currentTextChanged, this, [=](const QString &currentText){
        // 历史列表选中项发生改变将取出最新选中，设置到当前控件与样式框
        m_currentWidget->setStyleSheet(widgetStyleMaps[m_currentWidget][currentText]);
        ui->styleTextEdit->setPlainText(widgetStyleMaps[m_currentWidget][currentText]);
    });
    // 添加按钮
    connect(ui->historyListAddBtn, &QPushButton::clicked, this, [=](){
        // 历史列表将添加新样式，存储当前控件样式，并重选中列表新项
        QString newStyle("样式");

        int len = widgetStyleMaps[m_currentWidget].keys().length();
        if (!len == 0) {
            newStyle.append(QString::number(len));
        }
        ui->historyList->addItem(newStyle);
        widgetStyleMaps[m_currentWidget][newStyle]=m_currentWidget->styleSheet();

        ui->historyList->setCurrentRow(len);
        ui->historyListDelBtn->setEnabled(true);
    });
    // 删除按钮
    connect(ui->historyListDelBtn, &QPushButton::clicked, this, [=](){
        if (ui->historyList->count() == 0) return;                          // 不允许在空列表中进行删除: 引发段错误
        if (ui->historyList->selectedItems().length() == 0) return;         // 不允许将未选中项进行删除: 引发段错误
        // 历史列表将当前选中项踢出，并踢出当前控件样式列表的当前样式键
        QString itemText = ui->historyList->currentItem()->text();
        ui->historyList->takeItem(ui->historyList->currentRow());
        widgetStyleMaps[m_currentWidget].remove(itemText);
        if (ui->historyList->count() == 0) ui->historyListDelBtn->setEnabled(false);
    });

    // 颜色按钮
    connect(ui->colorBtn, &QPushButton::clicked, this, [=](){
        // 当前颜色被设置到对话框
        QString color = ui->colorBtn->property("color").toString();

        QColorDialog dialog(this);
        dialog.setCurrentColor(QColor(color));
        dialog.setWindowTitle(QStringLiteral("颜色选择对话框"));

        if (dialog.exec() == QColorDialog::Accepted) {
            // 从对话框中取得颜色并存入剪切板，并重设颜色按钮
            QColor color = dialog.selectedColor();
            QClipboard *clipboard = qApp->clipboard();
            clipboard->setText(color.name());

            this->setColorBtnColor(color.name());
        }
    });

    changeCurrentWidget("MainWindow");
    emit ui->fontSizeSlider->valueChanged(11);
}
MainWindow::~MainWindow()
{

}

void MainWindow::changeCurrentWidget(const QString &text)
{
//    hiddenAll();
    for (auto &key : widgets.keys())
    {
        if (key == text) {
            m_currentWidget = widgets[key];
            break;
        }
    }

    m_currentWidget->show();
    loadCurrentStyleSheet();
}

void MainWindow::loadCurrentStyleSheet(){
    ui->styleTextEdit->setPlainText(m_currentWidget->styleSheet());
}

void MainWindow::hiddenAll()
{
    for (auto &key : widgets.keys())
    {
        widgets[key]->hide();
    }
}

void MainWindow::setColorBtnColor(QString color)
{
    QColor c(color);
    QColor invertColor;
    invertColor.setRgb(255 - c.red(), 255 - c.green(), 255 - c.blue());
    ui->colorBtn->setProperty("color", color);
    ui->colorBtn->setStyleSheet(QString("background-color: %1; color: %2;").arg(color).arg(invertColor.name()));
}
